package com.sfh.dog.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SharedPerferces 工具 初始化时需配置使用参数 initConfig(SharedConfig mSharedConfig)
 * 
 * @author Swer
 * @version [版本号2014-2-27]
 */
public class SharedPreferencesUtils
{
	/**
	 * 配置参数
	 * 
	 * @author SunFeihu 孙飞虎
	 * @company 阿法贝多媒体有限公司
	 * @version [版本号2014-2-27]
	 */
	public static class SharedConfig
	{
		private Context _mContext;
		private String _mSharedName;
		private int _mMode;

		/**
		 * 设置应用活动对象
		 * 
		 * @param _mContext
		 * @return
		 */
		public SharedConfig setContext(Context _mContext)
		{
			this._mContext = _mContext;
			return this;
		}

		/***
		 * 设置sharedPerference 文件名
		 * 
		 * @param _mSharedName
		 * @return
		 */
		public SharedConfig setSharedName(String _mSharedName)
		{
			this._mSharedName = _mSharedName;
			return this;
		}

		/**
		 * 设置sharedPerference 类型
		 * 
		 * @param _mMode
		 * @return
		 */
		public SharedConfig setMode(int _mMode)
		{
			this._mMode = _mMode;
			return this;
		}
	}

	private SharedPreferences mSettings;
	private SharedPreferences.Editor mSettingsEditor;
	// 参数配置对象
	private SharedConfig _mConfig;

	// 内部静态对象创建
	private static class Hondler
	{
		private static SharedPreferencesUtils instance = new SharedPreferencesUtils();
	}

	/**
	 * 初始化时需配置使用参数 initConfig(SharedConfig mSharedConfig)
	 * 
	 * @return
	 */
	public static SharedPreferencesUtils getInstance()
	{
		return Hondler.instance;
	}

	/**
	 * 设置配置参数
	 * 
	 * @param mSharedConfig
	 */
	public void initConfig(SharedConfig mSharedConfig)
	{
		this._mConfig = mSharedConfig;
		this.mSettings = _mConfig._mContext.getSharedPreferences(
				_mConfig._mSharedName, _mConfig._mMode);
		this.mSettingsEditor = mSettings.edit();
	}

	private SharedPreferencesUtils()
	{
	}

	/** 清空数据*/
	public void clearPreference()
	{
		mSettingsEditor.clear();
		mSettingsEditor.commit();
	}

	public boolean commit()
	{
		if (mSettingsEditor == null)
		{
			return false;
		}
		return mSettingsEditor.commit();
	}

	public boolean getBoolean(final String entry, boolean defaultValue)
	{
		if (mSettingsEditor == null)
		{
			return defaultValue;
		}
		try
		{
			return mSettings.getBoolean(entry.toString(), defaultValue);
		} catch (Exception e)
		{
			e.printStackTrace();
			return defaultValue;
		}
	}

	public float getFloat(final String entry, float defaultValue)
	{
		if (mSettingsEditor == null)
		{
			return defaultValue;
		}
		try
		{
			return mSettings.getFloat(entry.toString(), defaultValue);
		} catch (Exception e)
		{
			e.printStackTrace();
			return defaultValue;
		}
	}

	public int getInt(final String entry, int defaultValue)
	{
		if (mSettingsEditor == null)
		{
			return defaultValue;
		}
		try
		{
			return mSettings.getInt(entry.toString(), defaultValue);
		} catch (Exception e)
		{
			e.printStackTrace();
			return defaultValue;
		}
	}

	public SharedPreferences getSharedPreferences()
	{
		return mSettings;
	}

	public String getString(final String entry, String defaultValue)
	{
		if (mSettingsEditor == null)
		{
			return defaultValue;
		}
		try
		{
			return mSettings.getString(entry.toString(), defaultValue);
		} catch (Exception e)
		{
			e.printStackTrace();
			return defaultValue;
		}
	}

	public boolean putBoolean(final String entry, boolean value)
	{
		return putBoolean(entry, value, false);
	}

	public boolean putBoolean(final String entry, boolean value, boolean commit)
	{
		if (mSettingsEditor == null)
		{
			return false;
		}
		mSettingsEditor.putBoolean(entry.toString(), value);
		if (commit)
		{
			return mSettingsEditor.commit();
		}
		return true;
	}

	public boolean putFloat(final String entry, float value)
	{
		return putFloat(entry, value, false);
	}

	public boolean putFloat(final String entry, float value, boolean commit)
	{
		if (mSettingsEditor == null)
		{
			return false;
		}
		mSettingsEditor.putFloat(entry.toString(), value);
		if (commit)
		{
			return mSettingsEditor.commit();
		}
		return true;
	}

	public boolean putInt(final String entry, int value)
	{
		return putInt(entry, value, false);
	}

	public boolean putInt(final String entry, int value, boolean commit)
	{
		if (mSettingsEditor == null)
		{
			return false;
		}
		mSettingsEditor.putInt(entry.toString(), value);
		if (commit)
		{
			return mSettingsEditor.commit();
		}
		return true;
	}

	public boolean putString(final String entry, String value)
	{
		return putString(entry, value, false);
	}

	public boolean putString(final String entry, String value, boolean commit)
	{
		if (mSettingsEditor == null)
		{
			return false;
		}
		mSettingsEditor.putString(entry.toString(), value);
		if (commit)
		{
			return mSettingsEditor.commit();
		}
		return true;
	}

	public boolean remove(String key, boolean commit)
	{
		if (mSettingsEditor == null)
		{
			return false;
		}
		mSettingsEditor.remove(key);
		if (commit)
		{
			return mSettingsEditor.commit();
		}

		return true;
	}

}
